/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.OrderParameterCarrier;
import com.minierp.bean.QueryParameterCarrier;
import com.minierp.bean.QueryResult;
import com.minierp.bean.UserBean;
import com.minierp.dao.AccessoryDao;
import com.minierp.dao.BbsArticleDao;
import com.minierp.dao.BbsColumnDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.QueryDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BbsArticle;
import com.minierp.model.BbsColumn;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class BbsArticleMACtr extends MultiActionController {
	private QueryDao queryDao;
	private BbsArticleDao bbsArticleDao;
	private BbsColumnDao bbsColumnDao;
	private AccessoryDao accessoryDao;
	private IdGenerator idGenerator;
	private String topicListView;
	private String replyListView;

	public ModelAndView handleTopicList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.topicListView);
		String columnId = request.getParameter("column_id");
		String searchKey = request.getParameter("search_key");
		mv.addObject("column", this.bbsColumnDao.get(BbsColumn.class,
				Long.parseLong(columnId)));
		mv.addObject("searchKey", searchKey);
		int total = this.bbsArticleDao.getTopListCount(
				Long.parseLong(columnId), searchKey);
		int perPageNum = EncCommonHelper.getPageSize(request);
		int pageno = EncCommonHelper.getCurrentPageIndex(request, total,
				perPageNum);
		int startIndex = (pageno - 1) * perPageNum;
		List list = this.bbsArticleDao.getTopList(Long.parseLong(columnId),
				searchKey, startIndex, perPageNum);
		mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total,
				perPageNum, pageno));
		return mv;
	}

	public ModelAndView handleReplyList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.replyListView);
		String columnId = request.getParameter("column_id");
		mv.addObject("column", this.bbsColumnDao.get(BbsColumn.class,
				Long.parseLong(columnId)));
		String topicId = request.getParameter("topic_id");
		mv.addObject("topicId", topicId);
		String objName = "com.minierp.model.BbsArticle";
		String objShortName = "b";
		HashMap shortNameMap = new HashMap();
		String[] retObjs = new String[]{objShortName};
		Vector qpVe = new Vector();
		shortNameMap.put(objName, objShortName);
		QueryParameterCarrier qp = new QueryParameterCarrier();
		qp.objectName = objName;
		qp.propertyName = "parentId";
		qp.value = " (b.id=" + topicId + " or b.parentId=" + topicId + ")";
		qp.dataType = 6;
		qp.logicType = 10;
		qpVe.add(qp);
		QueryParameterCarrier[] qParas = new QueryParameterCarrier[qpVe.size()];

		for (int p1 = 0; p1 < qpVe.size(); ++p1) {
			qParas[p1] = (QueryParameterCarrier) qpVe.get(p1);
		}

		OrderParameterCarrier arg16 = new OrderParameterCarrier();
		arg16.objectName = objName;
		arg16.propertyName = "id";
		arg16.orderTarget = "asc";
		OrderParameterCarrier[] oParas = new OrderParameterCarrier[]{arg16};
		int currentPage = EncCommonHelper.getCurrentPageIndex(request);
		QueryResult qr = this.queryDao.doQuery(currentPage,
				EncCommonHelper.getPageSize(request), qParas, oParas,
				(String[]) null, shortNameMap, retObjs, (String[]) null);
		mv.addObject("qr", qr);
		this.bbsArticleDao.update(Long.parseLong(topicId), 1);
		if (qr.getPageIndex() == 1) {
			mv.addObject("accList", this.accessoryDao.getList("BBS",
					Long.parseLong(topicId), (String[]) null, 0L));
		}

		return mv;
	}

	public ModelAndView handleReply(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String content = request.getParameter("content");
		String parentId = request.getParameter("parent_id");
		String columnId = request.getParameter("column_id");
		if (content != null && !content.equals("")) {
			BbsArticle mv = new BbsArticle();
			mv.setId(this.idGenerator.getId(BbsArticle.class.getName()));
			mv.setContent(content);
			mv.setColumnId(Long.parseLong(columnId));
			mv.setParentId(Long.parseLong(parentId));
			UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
			String stafferName = "";
			long stafferId = 0L;
			if (ub.getStaffer() != null) {
				stafferName = ub.getStaffer().getStafferName();
				stafferId = ub.getStaffer().getId();
			}

			mv.setCreaterId(stafferId);
			mv.setCreaterName(stafferName);
			mv.setCreateTime(new Date());
			mv.setLastTime(mv.getCreateTime());
			this.bbsArticleDao.save(mv);
			this.bbsArticleDao.update(mv.getParentId(), stafferId, stafferName,
					mv.getCreateTime());
		}

		EncCommonHelper.setAlertMessage(request, "回复保存成功");
		ModelAndView mv1 = new ModelAndView(
				"redirect:/bbs/BbsTopicList.ihtm?column_id=" + columnId);
		return mv1;
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("redirect:/bbs/BbsColumnList.ihtm");
		String id = request.getParameter("id");
		BbsArticle a = (BbsArticle) this.bbsArticleDao.get(BbsArticle.class,
				Long.parseLong(id));
		if (a != null) {
			if (a.getParentId() == 0L) {
				this.accessoryDao.delete("BBS", a.getId());
				this.bbsArticleDao.deleteTree(a.getId());
				mv = new ModelAndView(
						"redirect:/bbs/BbsTopicList.ihtm?column_id="
								+ a.getColumnId());
			} else {
				this.bbsArticleDao.update(a.getId(), false);
				mv = new ModelAndView(
						"redirect:/bbs/BbsReplyList.ihtm?column_id="
								+ a.getColumnId() + "&topic_id="
								+ a.getParentId());
			}

			EncCommonHelper.setAlertMessage(request, "删除成功");
		}

		return mv;
	}

	public ModelAndView handleTopicDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String fromUrl = request.getParameter("from_url");
		String[] arr = id.split(";");

		for (int i = 0; i < arr.length; ++i) {
			this.accessoryDao.delete("BBS", Long.parseLong(arr[i]));
			this.bbsArticleDao.deleteTree(Long.parseLong(arr[i]));
		}

		EncCommonHelper.setAlertMessage(request, "删除成功");
		return new ModelAndView("redirect:" + fromUrl);
	}

	public BbsArticleDao getBbsArticleDao() {
		return this.bbsArticleDao;
	}

	public void setBbsArticleDao(BbsArticleDao bbsArticleDao) {
		this.bbsArticleDao = bbsArticleDao;
	}

	public BbsColumnDao getBbsColumnDao() {
		return this.bbsColumnDao;
	}

	public void setBbsColumnDao(BbsColumnDao bbsColumnDao) {
		this.bbsColumnDao = bbsColumnDao;
	}

	public QueryDao getQueryDao() {
		return this.queryDao;
	}

	public void setQueryDao(QueryDao queryDao) {
		this.queryDao = queryDao;
	}

	public String getReplyListView() {
		return this.replyListView;
	}

	public void setReplyListView(String replyListView) {
		this.replyListView = replyListView;
	}

	public String getTopicListView() {
		return this.topicListView;
	}

	public void setTopicListView(String topicListView) {
		this.topicListView = topicListView;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public AccessoryDao getAccessoryDao() {
		return this.accessoryDao;
	}

	public void setAccessoryDao(AccessoryDao accessoryDao) {
		this.accessoryDao = accessoryDao;
	}
}